# !/usr/bin/env python
# -*- coding: utf-8 -*-
# 随机森林

import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression  # 导入逻辑回归模型
from sklearn.metrics import f1_score, confusion_matrix  # 导入评估标准
from sklearn.model_selection import train_test_split  # 拆分训练集和测试集
# 进行特征缩放
from sklearn import preprocessing

if __name__ == '__main__':
    df = pd.read_csv("data/example.csv")
    df.head()
    # 构建特征和标签集
    y = df.purchase.values
    X = df.drop(['purchase'], axis=1)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
    scaler = preprocessing.MinMaxScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)

    lr = LogisticRegression()  # 逻辑回归
    lr.fit(X_train, y_train)  # 训练模型
    y_pred = lr.predict(X_test)  # 预测结果
    lr_acc = lr.score(X_test, y_test) * 100  # 准确率
    lr_f1 = f1_score(y_test, y_pred) * 100  # F1分数
    print("逻辑回归测试集准确率： {:.2f}%".format(lr_acc))
    print("逻辑回归测试集F1分数: {:.2f}%".format(lr_f1))
    print('逻辑回归测试集混淆矩阵:\n', confusion_matrix(y_test, y_pred))
